package com.logos.servlet.req;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/req1")
public class RequestTest01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 获取请求行数据
         */
        // String getMethod(); 获取请求方式：GET
        System.out.println("getMethod: " + req.getMethod());
        // String getContextPath(); 获取虚拟目录：/study-servlet
        System.out.println("getContextPath: " + req.getContextPath());
        // StringBuffer getRequestURL(); 获取请求的统一资源定位符（绝对路径）：http://localhost:8080/study-servlet/req1
        System.out.println("getRequestURL: " + req.getRequestURL());
        // String getRequestURI(); 获取请求的资源路径：/study-servlet/req1
        System.out.println("getRequestURI: " + req.getRequestURI());
        // String getQueryString(); 获取请求的参数字符串：name=logos&age=19
        System.out.println("getQueryString: " + req.getQueryString());
        /**
         * 获取请求头数据
         */
        // String getHeader(String name); 获取指定的请求头数据
        System.out.println("getHeader: " + req.getHeader("User-Agent"));
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 获取请求体数据
         */
        // 1.获取字节输入流
        BufferedReader br = req.getReader();
        // 2.读取数据
        String line = br.readLine();
        System.out.println("line = " + line);
    }
}
